package com.q.tqb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.q.tqb.R;
import com.q.tqb.model.RedListData;
import com.q.tqb.view.CircleImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GrabRedEnvelopeAdapter extends RecyclerView.Adapter<GrabRedEnvelopeAdapter.ViewHolder> {
    private ArrayList<RedListData> mRedListData;
    private Context mContext;


    public GrabRedEnvelopeAdapter(Context context, ArrayList<RedListData> mRedListData) {

        this.mRedListData = mRedListData;
        this.mContext = context;


    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_grab_red, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        RedListData redListData = mRedListData.get(position);

        Glide
                .with(mContext)
                .load(redListData.getHeader())
                .error(R.drawable.ic_head)
                .placeholder(R.drawable.ic_head)
                .dontAnimate()
                .into(holder.mIvHead);


        holder.mTvName.setText(redListData.getNickname());
        holder.mTvMoney.setText("共" + redListData.getPacket_money() + "元");
        holder.mTvAmount.setText(redListData.getGet_num() + "/" + redListData.getPacket_num() + "个");

        if (redListData.getIs_users_agent() == 1) {
            holder.mIvSex.setVisibility(View.VISIBLE);
        } else {
            holder.mIvSex.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(redListData.getPacket_content())) {
            holder.mTvContent.setText(redListData.getPacket_title());
        } else {
            holder.mTvContent.setText(redListData.getPacket_content());
        }

        if (redListData.getGet_num() == redListData.getPacket_num()) {
            holder.mIvRedFinish.setVisibility(View.VISIBLE);
        } else {
            holder.mIvRedFinish.setVisibility(View.GONE);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onClick(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mRedListData.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_content)
        TextView mTvContent;
        @BindView(R.id.tv_money)
        TextView mTvMoney;
        @BindView(R.id.tv_amount)
        TextView mTvAmount;
        @BindView(R.id.iv_head)
        CircleImageView mIvHead;
        @BindView(R.id.iv_vip)
        ImageView mIvSex;
        @BindView(R.id.iv_red_finish)
        ImageView mIvRedFinish;
        @BindView(R.id.tv_name)
        TextView mTvName;


        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}